/*
 * Copyright (C) 2014 Alpha, Inc.
 * This is NOT a freeware, use is subject to license terms.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cc.gehua.octopus.rpc.utils;

import com.google.common.base.Strings;

/**
 *
 * @author guna
 */
public class ConvertUtil {

    public static boolean String2Bool(String src) {
        return Strings.isNullOrEmpty(src) ? false : "true".equals(src.trim().toLowerCase());
    }

    public static boolean String2Bool(String src, boolean fail) {
        return Strings.isNullOrEmpty(src) ? fail : "true".equals(src.trim().toLowerCase());
    }

    public static int String2Int(String src) {
        return Integer.parseInt(src);
    }
    
    public static long String2Long(String src) {
        return Long.parseLong(src);
    }

    public static int String2Int(String src, int fail) {
        if(src == null || src.isEmpty()){
            return fail;
        }
        try {
            return Integer.parseInt(src);
        } catch (NumberFormatException e) {
            return fail;
        }
    }
    
    public static long String2Long(String src, long fail) {
        if(src == null || src.isEmpty()){
            return fail;
        }
        try {
            return Long.parseLong(src);
        } catch (NumberFormatException e) {
            return fail;
        }
    }
}
